﻿namespace Player.Views
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Common.Interfaces;
    using Common.Localization;
    using Common.Protocol;
    using Player.Controllers;
    using Player.Models;

    using UI;

    public partial class MainView : ViewBase
    {
        private ExtendedCell extendedCell;
        private bool extendedCellEnabled = false;
        private string protocolPath = string.Empty;

        private MainViewController controller;

        private MainViewModel model;

        public MainView()
        {
            this.controller = new MainViewController();
            this.InitializeComponent();
        }

        public override void Localize()
        {
            foreach (var control in this.Controls)
            {
                try
                {
                    (control as ILocalizable).Localize();
                }
                catch (NullReferenceException)
                {
                }
            }

            this.LocalizeToolStripMenu();
        }

        private void LocalizeToolStripMenu()
        {
            this.fileToolStripMenuItem.Text = LocalizationProvider.GetMessage("file");
            this.exitToolStripMenuItem.Text = LocalizationProvider.GetMessage("exit");
            this.openToolStripMenuItem.Text = LocalizationProvider.GetMessage("open");
        }

        #region toolStripMenu items events

        private void OpenToolStripMenuItemClick(object sender, EventArgs e)
        {
            MainViewModel model = this.controller.OpenFile();
            if (model != null)
            {
                this.model = model;
                this.Initialize();
            }
        }

        private void ExitToolStripMenuItemClick(object sender, EventArgs e)
        {

        }

        #endregion

        public void Initialize()
        {
            this.cellContainer.Clear();
            foreach (var channel in this.model.currentInterval.ChannelIntervals)
            {
                if (channel.IsActive)
                {
                    Cell cell = new Cell(CellType.Active);
                    cell.Color = channel.Config.Color;
                    cell.HeaderText = channel.Config.Name;
                    double value;
                    this.model.CurrentValues.TryGetValue(channel.Config.Name, out value);
                    cell.Value = (float)value;
                    this.cellContainer.AddCell(cell);
                }
            }

            this.chart.Initialize(this.model.Intervals, this.model.TotalMilliseconds);
        }

        #region extended cell

        private void ShowExtendedCell(object sender, EventArgs e)
        {
            if (this.extendedCellEnabled)
            {
                this.extendedCell.Cell = sender as Cell;
            }
            else
            {
                this.extendedCellEnabled = true;
                this.extendedCell = new ExtendedCell(sender as Cell);
                int top = cellContainer.Top;
                int left = cellContainer.Left;
                this.extendedCell.Top = top;
                this.extendedCell.Left = left;
                this.extendedCell.Close += this.HideExtendedCell;
                this.cellContainer.Top = this.extendedCell.Bottom + 2 * Cell.Margin;
                this.cellContainer.Height = this.ClientRectangle.Height - this.extendedCell.Height
                    - this.menuStrip1.Height - 2 * Cell.Margin;
                this.Controls.Add(this.extendedCell);
            }
        }

        private void HideExtendedCell(object sender, EventArgs e)
        {
            this.extendedCellEnabled = false;
            this.Controls.Remove(this.extendedCell);
            this.extendedCell = null;

            this.cellContainer.Top = this.menuStrip1.Bottom;
            this.cellContainer.Height = this.ClientRectangle.Height - this.menuStrip1.Height;
        }

        #endregion

        private void CellContainerChannelDeactivated(object sender, Cell e)
        {
            foreach (var interval in this.model.Intervals)
            {
                foreach (var channel in interval.ChannelIntervals)
                {
                    if (channel.Config.Name == e.HeaderText)
                    {
                        channel.IsActive = false;
                        this.chart.Initialize(this.model.Intervals, this.model.TotalMilliseconds);
                        break;
                    }
                }    
            }
        }
    }
}